package cn.itcast.service.impl;

import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import cn.itcast.vo.OrderSettingVo;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.*;

/**
 * @author FH
 * @createTime 2021年11月15日 15:18:00
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {


    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 预约设置查询的实现
     *
     * @param orderDate
     * @return
     */
    @Override
    public List<OrderSettingVo> getOrderSettingsByMonth(String orderDate) {
        log.info("--预约设置查询的实现:{}", orderDate);
            orderDate = orderDate + "-";
        try {
//            Date date = DateUtils.parseString2Date(orderDate,"yyyy-MM-dd");
            List<OrderSetting> settings = this.orderSettingDao.findAllByOrderDate(orderDate);
            log.info("查询的结果:{}", settings);
            List<OrderSettingVo> list = new ArrayList<>();
            if (null != settings) {
                for (OrderSetting setting : settings) {
                    OrderSettingVo orderSettingVo = new OrderSettingVo();
                    orderSettingVo.setDate(setting.getOrderDate().getDate());
                    log.info("OrderDate:{}",setting.getOrderDate().getDate());
                    orderSettingVo.setNumber(setting.getNumber());
                    orderSettingVo.setReservations(setting.getReservations());

                    list.add(orderSettingVo);
                }
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 单日预约设置实现
     * @param orderDate 当前日期
     * @param number  预约数
     * @return
     */
    @Override
    public Boolean updateOrderSettingNumber(Date orderDate,Integer number) {
        log.info("单日预约设置实现:{}",orderDate);
        //先判断,当前日期数据库中是否已有数据,有就根据日期进行修改
        try {
            OrderSetting orderSettings = this.orderSettingDao.findAllByOrderDateOne(orderDate);
            if (null != orderSettings){
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                this.orderSettingDao.updateOrderSettingNumber(orderSetting);
                return true;
            }else {
                //没有就将根据当前日期插入一条数据
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                orderSetting.setReservations(orderSetting.getReservations());
                this.orderSettingDao.insertOrderSettingNumber(orderSetting);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 批量设置预约数的实现
     * @param readExcelList
     */
    @Override
    public Boolean upload(List<String[]> readExcelList) {
        log.info("批量设置预约数的实现:{}",readExcelList);
        try {
            //先取出表中日期,再根据当前日期判断数据库中是否已有数据,有就根据日期进行修改
            for (String[] strs : readExcelList) {
                Date date = DateUtils.parseString2Date(strs[0]);
                log.info("日期:{}",date);
                Integer number = Integer.valueOf(strs[1]);
                OrderSetting orderSettingDate = this.orderSettingDao.findAllByOrderDateOne(date);
                log.info("查询MySQL库的结果:{}",orderSettingDate);
                if (null != orderSettingDate){
                    orderSettingDate.setOrderDate(date);
                    orderSettingDate.setNumber(number);
                    orderSettingDate.setReservations(orderSettingDate.getReservations());
                    this.orderSettingDao.updateOrderSettingNumber(orderSettingDate);
                }else {
                    //没有就将根据当前日期插入一条数据
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setOrderDate(date);
                    orderSetting.setNumber(number);
                    this.orderSettingDao.insertOrderSettingNumber(orderSetting);
                }
            }
            return true;
        } catch (Exception e) {
           log.error("上传失败!",e);
        }
        return false;
    }


}
